package com.gopay.bis.manager.bank.expay;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import com.gopay.common.constants.bank.ExtPayBankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPay;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPayAgreement;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPayOrderment;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.businesses.ExPayService;

/**
 * @TBankB2cExpayManager.java
 * 
 * @author WangHongFei
 */
public class TBankExPayManager {

	@Resource(name = "tbankExPayServiceClient")
	private ExPayService tbankExPayServiceClient;
	@Resource(name = "tbankConfig")
	private Map<String, String> tbankConfig;

	/**
	 * 快捷签约
	 * 
	 * @param ExPayBaseMessage
	 * @return ExPayBaseMessage
	 */
	public ExPayBaseMessage signAgree(ExPayBaseMessage exPayBaseMessage) throws GopayException{
		try{
			String orderDate14 = DateUtils.getLocalFullDateTime14();
			Map exPayHashMap = exPayBaseMessage.getExPayHashMap();
			exPayHashMap.put("msgType", "5101");
			exPayHashMap.put("orderNo", exPayBaseMessage.getOrderId());
			exPayHashMap.put("mercNo",(String) tbankConfig.get("TBANK.B2C.MerchantNo"));
			exPayHashMap.put("txnType",(String) tbankConfig.get("TBANK.B2C.TXNTYPE"));
			exPayHashMap.put("orderUrl",(String) tbankConfig.get("TBANK.EXPAY.NOTIFY.URL"));
			exPayHashMap.put("orderSerUrl",(String) tbankConfig.get("TBANK.EXPAY.NOTIFY.MERURL"));
			exPayHashMap.put("orderDate", orderDate14.substring(0, 8));
			exPayHashMap.put("orderTime",orderDate14.substring(8, orderDate14.length()));
			exPayHashMap.put("orderNote", "tbank");
			exPayHashMap.put("signData", "tbankSign");
			exPayHashMap.put("bankUrl",(String) tbankConfig.get("TBANK.MTPAY.BANKURL"));
			return exPayBaseMessage;
		}catch(Exception e){
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6053);
		}
	}

	/**
	 * 快捷解约
	 * 
	 * @param ExPayBaseMessage
	 * @return ExPayBaseMessage
	 * @throws Exception
	 */
	public ExPayBaseMessage cancelAgree(ExPayBaseMessage exPayBaseMessage)
			throws GopayException {
		String orderDate14 = DateUtils.getLocalFullDateTime14();
		Map<String, String> exPayHashMap = new ConcurrentHashMap<String, String>();
		TBankMessageExPayAgreement tbankExPayMsg = new TBankMessageExPayAgreement();
		exPayHashMap.put("msgType", "5201");
		tbankExPayMsg.setExPayHashMap(exPayHashMap);
		tbankExPayMsg.setAgrmtNo(exPayBaseMessage.getAgrmtNo());
		tbankExPayMsg.setOrderId(String.valueOf(System.currentTimeMillis()));
		tbankExPayMsg.setMercNo((String) tbankConfig.get("TBANK.B2C.MerchantNo"));
		tbankExPayMsg.setExPayOperType(ExtPayBankConstants.EXTPAY_CANCELAGREEMENT);
		tbankExPayMsg.setTxnType((String) tbankConfig.get("TBANK.B2C.TXNTYPE"));
		tbankExPayMsg.setOrderDate(orderDate14);
		tbankExPayMsg.setOrderNote("tbank");
		tbankExPayMsg.setSignData("tbankSign");
		tbankExPayMsg.setBankUrl((String) tbankConfig.get("TBANK.MTPAY.BANKURL"));
		TBankMessageExPay tBankMessageExPay = null;
		tBankMessageExPay = (TBankMessageExPay) tbankExPayServiceClient.queryOrder(tbankExPayMsg);
		return tBankMessageExPay;
	}

	/**
	 * 快捷支付
	 * 
	 * @param ExPayBaseMessage
	 * @return ExPayBaseMessage
	 * @throws Exception
	 */
	public ExPayBaseMessage payOrder(ExPayBaseMessage exPayBaseMessage)
			throws GopayException {
		TBankMessageExPayOrderment tbankOrderMsg = new TBankMessageExPayOrderment();
		Map<String, String> exPayHashMap = new ConcurrentHashMap<String, String>();
		exPayHashMap.put("msgType", "0501");
		tbankOrderMsg.setExPayHashMap(exPayHashMap);
		tbankOrderMsg.setMercNo((String) tbankConfig.get("TBANK.B2C.MerchantNo"));
		tbankOrderMsg.setOrderId(exPayBaseMessage.getOrderId());
		tbankOrderMsg.setExPayOperType(exPayBaseMessage.getExPayOperType());
		tbankOrderMsg.setTxnType((String) tbankConfig.get("TBANK.B2C.TXNTYPE"));
		tbankOrderMsg.setAgrmtNo(exPayBaseMessage.getAgrmtNo());
		tbankOrderMsg.setCurCode((String) tbankConfig.get("TBANK.CUR.CODE"));
		tbankOrderMsg.setAmount(exPayBaseMessage.getAmount());
		tbankOrderMsg.setOrderDate(exPayBaseMessage.getOrderDate());
		tbankOrderMsg.setOrderNote("123");
		tbankOrderMsg.setSignData("123");
		tbankOrderMsg.setBankUrl((String) tbankConfig.get("TBANK.MTPAY.BANKURL"));
		TBankMessageExPay tBankMessageExPay = null;
		if (getFailExpay(exPayBaseMessage)) {
			tBankMessageExPay = new TBankMessageExPay();
			tBankMessageExPay.setIsTimeOut(false);
			return tBankMessageExPay;
		} else if (getTimeoutExpay(exPayBaseMessage)) {
			tBankMessageExPay = new TBankMessageExPay();
			tBankMessageExPay.setIsTimeOut(true);
			return tBankMessageExPay;
		}
		tBankMessageExPay = (TBankMessageExPay) this.tbankExPayServiceClient.queryOrder(tbankOrderMsg);
		return tBankMessageExPay;
	}

	/**
	 * 快捷查询
	 * 
	 * @param ExPayBaseMessage
	 * @return ExPayBaseMessage
	 * @throws Exception
	 */
	public ExPayBaseMessage queryOrder(ExPayBaseMessage exPayBaseMessage)
			throws GopayException {
		TBankMessageExPayOrderment tbankOrderMsg = new TBankMessageExPayOrderment();
		Map<String, String> exPayHashMap = new ConcurrentHashMap<String, String>();
		exPayHashMap.put("msgType", "0203");
		tbankOrderMsg.setExPayHashMap(exPayHashMap);
		tbankOrderMsg.setMercNo((String) tbankConfig.get("TBANK.B2C.MerchantNo"));
		tbankOrderMsg.setOrderId(exPayBaseMessage.getOrderId());
		tbankOrderMsg.setExPayOperType(exPayBaseMessage.getExPayOperType());
		tbankOrderMsg.setTxnType((String) tbankConfig.get("TBANK.B2C.TXNTYPE"));
		tbankOrderMsg.setSignData("123");
		tbankOrderMsg.setBankUrl((String) tbankConfig.get("TBANK.MTPAY.BANKURL"));
		TBankMessageExPay tBankMessageExPay = null;
		if (((String) tbankConfig.get("TBANK.EXPAY.TEST.TIMEOUT.TXN.ONE")).equals(exPayBaseMessage.getAmount().toString())) {
			tBankMessageExPay = new TBankMessageExPay();
			tBankMessageExPay.getExPayHashMap().put("retCd", "SSSSSS");
			return tBankMessageExPay;
		} else if (((String) tbankConfig.get("TBANK.EXPAY.TEST.TIMEOUT.TXN.TWO")).equals(exPayBaseMessage.getAmount().toString())) {
			tBankMessageExPay = new TBankMessageExPay();
			tBankMessageExPay.setIsTimeOut(false);
			return tBankMessageExPay;
		}
		tBankMessageExPay = (TBankMessageExPay) this.tbankExPayServiceClient.queryOrder(tbankOrderMsg);
		return tBankMessageExPay;
	}
	
	/** 测试使用 */
	private boolean getFailExpay(ExPayBaseMessage exPayBaseMessage) {
		boolean flag = false;
		String failTxn = (String) tbankConfig.get("TBANK.EXPAY.TEST.FAIL.TXN");
		if (failTxn.equals(exPayBaseMessage.getAmount().toString())) {
			flag = true;
		}
		return flag;
	}

	/** 测试使用 */
	private boolean getTimeoutExpay(ExPayBaseMessage exPayBaseMessage) {
		boolean flag = false;
		String timeOutTxn1 = (String) tbankConfig.get("TBANK.EXPAY.TEST.TIMEOUT.TXN.ONE");
		String timeOutTxn2 = (String) tbankConfig.get("TBANK.EXPAY.TEST.TIMEOUT.TXN.TWO");
		if (timeOutTxn1.equals(exPayBaseMessage.getAmount().toString())) {
			flag = true;
		} else if (timeOutTxn2.equals(exPayBaseMessage.getAmount().toString())) {
			flag = true;
		}
		return flag;

	}
}
